//
//  LCYRadioPlayViewController.m
//  EPA
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "LCYRadioPlayViewController.h"
#import "LCYRadioDetailModel.h"
#import "LCYAVManager.h"

@interface LCYRadioPlayViewController ()
/// 标题
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
/// 播放进度
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
/// 当前时间
@property (weak, nonatomic) IBOutlet UILabel *currentTimeLable;
/// 总时间
@property (weak, nonatomic) IBOutlet UILabel *totalTimeLable;
/// 计时器
@property (nonatomic,strong) NSTimer *playTimer;
@property (weak, nonatomic) IBOutlet UIButton *playButton;

@property (weak, nonatomic) IBOutlet UIButton *nextBtn;


@end

@implementation LCYRadioPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.
    
    LCYRadioDetailModel *detailModel = self.radioArray[self.currentIndex];
    self.title = detailModel.title;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:detailModel.coverimg]];
    self.coverImageView.layer.cornerRadius = self.coverImageView.frame.size.width * 0.5;
    self.coverImageView.layer.masksToBounds = YES;
    [self addAnimation];
    
    [LCYAVManager shareManager].currentIndex = self.currentIndex;
    
    NSMutableArray *itemArray = [NSMutableArray new];
    for (LCYRadioDetailModel *detailModel in self.radioArray) {
        NSURL *musicUrl = [NSURL URLWithString:detailModel.playInfoModel.musicUrl];
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:musicUrl];
        [itemArray addObject:item];
    }
    
    //AVPlayerItem数组
    [LCYAVManager shareManager].itemArray = itemArray;
    // 播放
    [[LCYAVManager shareManager] play];
    
    
    // 创建timer
    self.playTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    
}

/// 添加动画
- (void)addAnimation {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    CGFloat point = M_PI_4;
    animation.values = @[@(point),@(2 * point),@(3 * point),@(4 * point),@(5 * point),@(6 * point),@(7 * point),@(8 * point),@(9 * point)];
    animation.repeatCount = MAXFLOAT;
    animation.duration = 5;
    [self.coverImageView.layer addAnimation:animation forKey:@"animation"];
}

- (void)timerAction {
    
    float current = [LCYAVManager shareManager].player.currentTime.value / [LCYAVManager shareManager].player.currentTime.timescale;
    NSString *currentStr = [NSString stringWithFormat:@"%02.02ld:%02.02ld",(NSInteger)current/60,(NSInteger)current%60];
    self.currentTimeLable.text = currentStr;
    
    
    float total = 0;
    if ([LCYAVManager shareManager].player.currentItem.duration.timescale > 0) {
        total = [LCYAVManager shareManager].player.currentItem.duration.value / [LCYAVManager shareManager].player.currentItem.duration.timescale;
    }
    NSString *totalStr = [NSString stringWithFormat:@"%02.02ld:%02.02ld",(NSInteger)total/60,(NSInteger)total%60];
    self.totalTimeLable.text = totalStr;
    
    if (total > 0) {
        float rate = current / total;
        self.rateSlider.value = rate;
        if (self.rateSlider.value == 1) {
            [self didClickNextButton:self.nextBtn];
        }
    }
    
}
#pragma mark - 改变播放进度
- (IBAction)didClickChangeRateSlider:(UISlider *)sender {
    CMTime changeTime = CMTimeMake(sender.value * [LCYAVManager shareManager].player.currentItem.duration.value, [LCYAVManager shareManager].player.currentItem.duration.timescale) ;
    // 调用seekToTime
    [[LCYAVManager shareManager] seekToTime:changeTime];
    
}

#pragma mark - 点击上一曲
- (IBAction)didClickPreviousButton:(UIButton *)sender {
    [[LCYAVManager shareManager] previous];
    // 图片和标题
    self.currentIndex--;
    if (self.currentIndex < 0) {
        self.currentIndex = self.radioArray.count - 1;
    }
    LCYRadioDetailModel *detailModel = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:detailModel.coverimg]];
    self.title = detailModel.title;
    
    [self.playButton setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
    
}
#pragma mark - 点击播放按钮
- (IBAction)didClickPlayButton:(UIButton *)sender {
    if ([LCYAVManager shareManager].player.rate == 0) {
        [[LCYAVManager shareManager] play];
        [sender setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
    } else {
        [[LCYAVManager shareManager] pause];
        [sender setImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
    }
}
#pragma mark - 点击下一曲
- (IBAction)didClickNextButton:(UIButton *)sender {
    [[LCYAVManager shareManager] next];
    // 图片和标题
    self.currentIndex++;
    if (self.currentIndex >= self.radioArray.count) {
        self.currentIndex = 0;
    }
    LCYRadioDetailModel *detailModel = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:detailModel.coverimg]];
    self.title = detailModel.title;
    
    [self.playButton setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
